﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;

namespace TuongLaiDangCho.Enterface
{
    public partial class frmChuyenNgay : DevExpress.XtraEditors.XtraForm
    {
        bool b_flagCM;
        public frmChuyenNgay()
        {
            InitializeComponent();
            deCopyMove.DateTime = DateTime.Today;
            b_flagCM = true;
        }

        private void ceMove_CheckedChanged(object sender, EventArgs e)
        {
            if (ceMove.Checked)
            {
                bntProccess.Text = "Move";
                b_flagCM = false;
            }
        }

        private void ceCopy_CheckedChanged(object sender, EventArgs e)
        {
            if (ceCopy.Checked)
            {
                bntProccess.Text = "Copy";
                b_flagCM = true;
            }
        }

        private void bntProccess_Click(object sender, EventArgs e)
        {

            switch (frmHome.b_flag)
            {
                case 0:
                    NhatKiDTO diary = new NhatKiDTO();
                    diary.Ngay = deCopyMove.DateTime;
                    if (b_flagCM)
                    {
                        if(!NhatKiDAO.IsDate(diary))
                        {
                            //XtraMessageBox.Show("true");
                            diary.Ngay=frmHome.choiseDate;
                            diary.Noidung = NhatKiDAO.LoadDiary(diary);
                            diary.Ngay = deCopyMove.DateTime;
                            NhatKiDAO.InsertDiary(diary);
                            this.Close();
                        }            
                        else
                        {
                            XtraMessageBox.Show("Dữ liệu đã tồn tại, bạn không thể ghi đè", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        if(!NhatKiDAO.IsDate(diary))
                        {
                            diary.Ngay = frmHome.choiseDate;
                            diary.Noidung = NhatKiDAO.LoadDiary(diary);
                            NhatKiDAO.DeleleDiary(diary);
                            diary.Ngay = deCopyMove.DateTime;
                            NhatKiDAO.InsertDiary(diary);
                            this.Close();
                        }
                        else
                        {
                            XtraMessageBox.Show("Dữ liệu đã tồn tại, bạn không thể ghi đè", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        //b_flag = 0;
                    }
                    break;
                case 2:
                    DieuMoiDTO news = new DieuMoiDTO();
                    news.NgayThang = frmHome.choiseDate;
                    if (b_flagCM)
                    {
                        DataTable dt = DieuMoiDAO.LoadNews(news);
                        int n=dt.Rows.Count;
                        news.NgayThang = deCopyMove.DateTime;
                        //XtraMessageBox.Show(deCopyMove.DateTime.ToShortDateString());
                        for (int i = 0; i < n; i++)
                        {
                            //news.NgayThang = (DateTime)dt.Rows[i]["NgayThang"];
                            news.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            news.GhiChu = dt.Rows[i]["GhiChu"].ToString();
                            DieuMoiDAO.InsertNews(news);
                            //XtraMessageBox.Show(deCopyMove.DateTime.ToShortDateString());
                        }
                        this.Close();
                    }
                    else
                    {
                        DataTable dt = DieuMoiDAO.LoadNews(news);
                        int n = dt.Rows.Count;
                        news.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            //news.NgayThang = (DateTime)dt.Rows[i]["NgayThang"];
                            news.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            news.GhiChu = dt.Rows[i]["GhiChu"].ToString();
                            news.Id = int.Parse(dt.Rows[i]["ID"].ToString());
                            DieuMoiDAO.InsertNews(news);
                            DieuMoiDAO.DeleteNews(news);
                        }                        
                        this.Close();
                        //b_flag = 2;
                    }
                    break;
                case 3:
                    ChiTieuDTO expend = new ChiTieuDTO();
                    expend.NgayThang = frmHome.choiseDate;
                    if (b_flagCM)
                    {
                        DataTable dt = ChiTieuDAO.LoadExpenditure(expend);
                        int n = dt.Rows.Count;
                        expend.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            expend.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            expend.SoTien = int.Parse(dt.Rows[i]["SoTien"].ToString());
                            ChiTieuDAO.InsertExpenditure(expend);
                        }
                        this.Close();
                    }
                    else
                    {
                        DataTable dt = ChiTieuDAO.LoadExpenditure(expend);
                        int n = dt.Rows.Count;
                        expend.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            expend.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            expend.SoTien = int.Parse(dt.Rows[i]["SoTien"].ToString());
                            expend.Id = int.Parse(dt.Rows[i]["ID"].ToString());
                            ChiTieuDAO.InsertExpenditure(expend);
                            ChiTieuDAO.DeleteExpenditure(expend);
                        }
                        this.Close();
                        //b_flag=
                    }
                    break;
                case 4:
                    LenLichDTO timetable = new LenLichDTO();
                    timetable.NgayThang = frmHome.choiseDate;
                    if (b_flagCM)
                    {
                        DataTable dt = LenLichDAO.LoadTimeTable(timetable);
                        int n = dt.Rows.Count;
                        timetable.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            timetable.Gio = dt.Rows[i]["Gio"].ToString();
                            timetable.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            //timetable.ID=int.Parse(dt.Rows[i]["ID"].ToString());
                            LenLichDAO.InsertTimeTable(timetable);
                            this.Close();
                        }
                    }
                    else
                    {
                        DataTable dt = LenLichDAO.LoadTimeTable(timetable);
                        int n = dt.Rows.Count;
                        timetable.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            timetable.Gio = dt.Rows[i]["Gio"].ToString();
                            timetable.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            timetable.ID=int.Parse(dt.Rows[i]["ID"].ToString());
                            LenLichDAO.InsertTimeTable(timetable);
                            LenLichDAO.DeleteTimeTable(timetable);
                            this.Close();
                        }
                    }
                    break;
                //case 4:
                //    //GhiChuDTO note = new GhiChuDTO();
                //    if (GhiChuDAO.LoadNote().Rows.Count <= 0)
                //    {
                //        XtraMessageBox.Show("Không có nội dung để chuyển", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //    }
                //    else
                //    {
                //        frmChuyenNgay _frmChuyenNgay = new frmChuyenNgay();
                //        _frmChuyenNgay.ShowDialog();
                //    }
                //    break;
                case 7:
                    QuanLiThoiGianDTO dataChar = new QuanLiThoiGianDTO();
                    dataChar.NgayThang = frmHome.choiseDate;
                    if (b_flagCM)
                    {
                        DataTable dt = QuanLiThoiGianDAO.LoadDataChar(dataChar);
                        int n = dt.Rows.Count;
                        dataChar.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            dataChar.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            dataChar.ThoiGian = int.Parse(dt.Rows[i]["ThoiGian"].ToString());
                            dataChar.HuuIch = dt.Rows[i]["HuuIch"].ToString();
                            QuanLiThoiGianDAO.InsertDataChar(dataChar);
                        }
                        this.Close();
                    }
                    else
                    {
                        DataTable dt = QuanLiThoiGianDAO.LoadDataChar(dataChar);
                        int n = dt.Rows.Count;
                        dataChar.NgayThang = deCopyMove.DateTime;
                        for (int i = 0; i < n; i++)
                        {
                            dataChar.NoiDung = dt.Rows[i]["NoiDung"].ToString();
                            dataChar.ThoiGian = int.Parse(dt.Rows[i]["ThoiGian"].ToString());
                            dataChar.HuuIch = dt.Rows[i]["HuuIch"].ToString();
                            dataChar.ID=int.Parse(dt.Rows[i]["ID"].ToString());
                            QuanLiThoiGianDAO.InsertDataChar(dataChar);
                            QuanLiThoiGianDAO.DeleteDataChar(dataChar);
                        }
                        this.Close();
                    }
                    break;
                default:
                    break;
            }
        }

    }
}